import {Component} from 'react'
import {withRouter} from 'react-router-dom'
import { Steps, Button, message } from 'antd';
import './AddAccount.css'
import BasicInfo from "../../components/hanqun/BasicInfo";
import SetLimit from "../../components/hanqun/SetLimit";
import axios from 'axios'
const { Step } = Steps;
const steps = [
    {
        title: '基本信息',
    },
    {
        title: '设置权限',
    }
];
class AddAccount extends Component{
    constructor() {
        super();
        this.isSuccess = true;     //是否全部添加成功
        this.state = {
            current:0,
            basicInfo:{},
            roleId:0
        }
    }
    onRef(ref){this.child = ref}
    //下一步
    next = () => {
        this.setState({
            basicInfo:this.refs.basic.sendBasicInfo()
        },()=>{
            //添加基本信息
            var info = this.state.basicInfo;
            if(info.adminName==undefined || info.adminPwd==undefined || info.createName==undefined){
                message.warning('请检查信息是否填写完整');
            }else {
                axios.post('/api/admin/admin/addDo', {
                    adminName:info.adminName,
                    adminPwd:info.adminPwd,
                    createName:info.createName,
                },{
                    headers:{
                        'Content-Type':'application/json;charset=UTF-8'
                    }
                }).then((res)=>{
                    if(res.data.code ===200){
                        //获取roleId
                        axios.get('/api/admin/admin/page').then((res)=>{
                            var data = res.data.data.list;
                            data.forEach(item=>{
                                if(item.adminName === info.adminName){
                                    this.setState({
                                        roleId: item.roles[0].roleId
                                    })
                                }
                            });
                        });
                        this.setState({
                            current:this.state.current+1
                        })
                    } else {       //添加错误
                        message.error(res.data.msg);
                        this.isSuccess = false
                    }
                });
            }

        });
    };
    //上一步
    prev = () => {
        this.setState({
            current:this.state.current-1
        })
    };
    //提交
    submit=()=>{
        //添加权限
        var arr = this.refs.limits.sendAllId();
        console.log('parent',arr);
        axios.get("/api/admin/admin/updateAuthDo", {
                params: {
                    roleId: this.state.roleId.toString(),
                    ids: arr.join(),
                },
                headers: {
                    "Content-Type": "application/x-www-form-urlencoded",
                }
            }).then((res) => {
                console.log(res);
                if (res.data.code == 200) {
                    message.success('添加成功');
                    this.props.history.push('/accountManage');
                }else {
                    message.err(res.data.msg);
                }
            });
    };
    render() {
        const comp = this.state.current;
        return (
            <>
                <div className='addBox'>

                    <Steps current={this.state.current}>
                        {steps.map(item => (
                            <Step key={item.title} title={item.title} />
                        ))}
                    </Steps>
                    <div className="steps-content">
                        {/*{this.props.children}*/}
                        {comp===0?<BasicInfo ref='basic'/>:<SetLimit ref='limits'/>}
                    </div>
                    <div className="steps-action">
                        {this.state.current < steps.length - 1 && (
                            <Button type="primary" onClick={this.next}>
                                {/*<Link to='/addAccount/setLimit'>下一步</Link>*/}
                                下一步
                            </Button>
                        )}
                        {this.state.current === steps.length - 1 && (
                            <Button type="primary" onClick={this.submit}>
                                {/*<Link to='/accountManage'>提交</Link>*/}
                                提交
                            </Button>
                        )}
                        {this.state.current > 0 && (
                            <Button style={{ margin: '0 8px' }} onClick={this.prev}>
                                {/*<Link to='/addAccount/basicInfo'>上一步</Link>*/}
                                上一步
                            </Button>
                        )}
                    </div>
                </div>
            </>
        );
    }
}

export default withRouter(AddAccount)
